import 'package:concert_ticket_client/app/model/resp/Resp.dart';
import 'package:concert_ticket_client/app/utils/data_persistence/localstorage.dart';
import 'package:concert_ticket_client/app_config.dart';
import 'package:dio/dio.dart';

class HttpUtil {
  static final String baseUrl = AppConfig.get("server")["baseUrl"];

  static dynamic get({
        required String urlSuffix,
        Map<String, dynamic>? params,
      }) async{
    Dio dio = Dio();
    //  响应结果类型转换
    String url = baseUrl + urlSuffix;
    Response response = await dio.request(
      url,
      options: Options(
        method: "GET",
        headers: {
          'Content-Type': 'application/json',
          'Authorization': await LocalStorage.getAuthToken() ?? '',
        },
      ),
      queryParameters:  params,
    );
    return R.fromJson(response.data);
  }

  static dynamic post({
        required String urlSuffix,
        Map<String, dynamic>? params,
        dynamic requestBody,
      }) async{
    Dio dio = Dio();
    String url = baseUrl + urlSuffix;
    return dio.request(
      url,
      options: Options(
        method: "POST",
        headers: {
          'Content-Type': 'application/json',
          'Authorization': await LocalStorage.getAuthToken() ?? '',
        },
      ),
      queryParameters:  params,
      data: requestBody,
    );
  }

  static dynamic put({
        required String urlSuffix,
        Map<String, dynamic>? params,
        dynamic requestBody,
      }) async{
    Dio dio = Dio();
    String url = baseUrl + urlSuffix;
    return dio.request(
      url,
      options: Options(
        method: "PUT",
        headers: {
          'Content-Type': 'application/json',
          'Authorization': await LocalStorage.getAuthToken() ?? '',
        },
      ),
      queryParameters:  params,
      data: requestBody,
    );
  }

  static dynamic delete({
        required String urlSuffix,
        Map<String, dynamic>? params,
        dynamic requestBody,
      }) async{
    Dio dio = Dio();
    String url = baseUrl + urlSuffix;
    return dio.request(
      url,
      options: Options(
        method: "DELETE",
        headers: {
          'Content-Type': 'application/json',
          'Authorization': await LocalStorage.getAuthToken() ?? '',
        },
      ),
      queryParameters:  params,
      data: requestBody,
    );
  }

}


